package com.docted.design.pattern.proxy.dynamic.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author DoCted
 * @since v1.0 2018/3/11
 */
public class JobAgent implements MethodInterceptor{


    public Object getInstance(Class<?> clazz) throws Exception {
        Enhancer enhancer = new Enhancer();
        //要把那个类设置为即将生成的类的父类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        //业务的增强处
        System.out.println("I am a JobAgent.");
        System.out.println("I got your reference. Let me know what you want.");
        methodProxy.invokeSuper(o, objects);
        System.out.println("Ok. No problem");
        return null;
    }
}
